<?php


namespace app\sys\model;


use app\admin\model\Chat;
use app\admin\model\Duty;
use app\sys\model\SensitiveWord as SensitiveWorkModel;
use sensitive\SensitiveWord as work;
use think\Db;
use think\Model;

class SensitiveWord extends Model
{
    protected $autoWriteTimestamp = true;
    public static function searchWord($data)
    {
        // 敏感词
        $disturbList = ['&', '*'];
        $SensitiveWorkList = SensitiveWorkModel::column("keyword");
        $wordObj = new work($disturbList);
        $wordObj->addWords($SensitiveWorkList);
        if($data) $data = $wordObj->filter($data);
        return $data;
    }

    public static function searchWordN($data,$userId,$staffId)
    {
        $disturbList = ['&', '*'];
        // 聊天提示
        $wordObj = new work($disturbList);
        $SensitiveWorkList = SensitiveWorkModel::column("keyword");
        $wordObj->addWords($SensitiveWorkList);
        $replaceCodeList = array();
        $wordsList = $wordObj->search($data, true, $replaceCodeList);
        if ($wordsList) {
            $data =  str_replace($wordsList, $replaceCodeList, $data);
            // 触发拦截
            chat::TisToUser($userId, $staffId, 0, "触发拦截");
        }
        return $data;
    }

    public static function addWord($staffNo,$keyword)
    {
        $arr = ["staff_no"=>$staffNo,"keyword"=>$keyword];
        SensitiveWord::create($arr);
    }

    public static function upWord($id,$staffNo,$keyword)
    {
        SensitiveWord::where("id",$id)->data(["keyword"=>$keyword])->update();
    }

    public static function ListWord($staffNo,$keyWord,$page,$limit):array
    {
        $s = SensitiveWord::field("id,keyword,create_time");
        if($keyWord){
            $s->where("keyword","like","%{$keyWord}%");
        }
        $c= $s->count();
        $list= $s->page($page,$limit)->order("id desc")->select();
        return  listDate($list,$c);
    }

    public static function DelWord($id = []){
        SensitiveWord::where("id","in",$id)->delete();
    }
}